package EnclosureUI;

import Communication.*;
import Database.*;
import Enclosure.Enclosure;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.sql.SQLException;

public class EnclosureControlPanel extends JFrame implements Runnable {
    final ZooDataHandler zooDataHandler = new ZooDataHandler();
    JPanel enclosureControlPanelView = new JPanel();
    EnclosureClient enclosureClient;
    Enclosure enclosure;

    public EnclosureControlPanel() throws Exception {
        this.setTitle("Enclosure Control Panel");
        this.setSize(415, 460);
        this.setLocation(850, 100);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        enclosureClient = new EnclosureClient(5555);
        enclosureClient.connectToServer();

        drawMenu();

        this.setVisible(true);
        this.run();
    }


    public void drawMenu() throws SQLException {
        JMenuBar menuBar = new JMenuBar();
        JMenu cageMenu  = new JMenu("Cages");
        menuBar.add(cageMenu);
        for (final Enclosure enclosure : zooDataHandler.getAllEnclosures()) {
            JMenuItem enclosureMenuItem = new JMenuItem(enclosure.getEnclosureName());
            enclosureMenuItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        switchCageView(enclosure);
                    } catch (Exception ex) {

                    }
                }
            });
            cageMenu.add(enclosureMenuItem);
            this.setJMenuBar(menuBar);
        }
    }


    public void switchCageView(Enclosure enclosure) {
        try {
            this.enclosure = enclosure;
            this.getContentPane().remove(enclosureControlPanelView);
            this.enclosureControlPanelView = new EnclosureControlPanelView(enclosure);

            enclosureControlPanelView.addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent e) {}
                @Override
                public void mousePressed(MouseEvent e) {}
                @Override
                public void mouseReleased(MouseEvent e) {
                    try {
                        enclosureClient.sendUpdate();
                    } catch (Exception err) {
                        System.out.println(err);
                    }
                }
                @Override
                public void mouseEntered(MouseEvent e) {}
                @Override
                public void mouseExited(MouseEvent e) {}
            });

            this.getContentPane().add(enclosureControlPanelView);
            this.repaint();
        } catch (IOException ioe) {
            System.out.println(ioe);
        }catch (Exception e) {
            System.out.println(e);
        }
    }


    @Override
    public void run() {
        try {
            while (true) {
                System.out.println("Client LISTENING");
                enclosureClient.receiveUpdate();
                System.out.println("SHOULD UPDATE HERE");
                switchCageView(zooDataHandler.getEnclosure(enclosure.getEnclosureId()));
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }


    public static void main(String[] args) throws Exception {
        new EnclosureControlPanel();
    }
}
